import path from "path";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import CopyPlugin from "copy-webpack-plugin";
import { copyDir } from "./webpack-utils.js";
import { mkdirs } from "./webpack-utils.js";
// import { default as webpack } from "webpack";
import nodeExternals from "webpack-node-externals";

const to = path.resolve("./dist/frontend");

mkdirs(to);

copyDir(path.resolve("src/fourth/frontend/frontend"), to);

const plugins = [
  new CleanWebpackPlugin({
    dangerouslyAllowCleanPatternsOutsideProject: true,
    dry: false,
  }),
  new CopyPlugin({
    patterns: [
      {
        from: path.resolve("src/fourth/backend/package.json"),
        to: path.resolve("dist/fourth/package.json"),
      },
    ],
  }),
];

export default {
  entry: path.resolve("./src/fourth/backend/index.ts"),
  target: "node",
  mode: "development",
  externals: [nodeExternals()],
  output: {
    path: path.resolve("./", "dist/fourth"),
    environment: {
      arrowFunction: false,
    },
  },
  resolve: {
    extensions: [".ts", ".js"], // Include TypeScript and JavaScript files
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
    ],
  },
  plugins,
};
